<template>
  <div style="padding:20px">
    <el-form ref="form" label-width="80px">

      <el-form-item label="课程标题">
        <el-input v-model="courseInfo.title"/>
      </el-form-item>

      <el-form-item label="课程讲师">
        <el-select v-model="courseInfo.teacherId" placeholder="请选择活动区域">
          <el-option
            v-for="teacher in teachers"
            :key="teacher.id"
            :label="teacher.name"
            :value="teacher.id"/>
        </el-select>
      </el-form-item>

      <el-form-item label="课程分类">
        <el-select
          v-model="courseInfo.subjectParentId"
          placeholder="请选择一级分类"
          @change="handleSubjectChange">
          <el-option
            v-for="pSubject in pSubjects"
            :key="pSubject.id"
            :label="pSubject.title"
            :value="pSubject.id"/>
        </el-select>
        <el-select v-model="courseInfo.subjectId" placeholder="请选择二级分类">
          <el-option
            v-for="cSubject in cSubjects"
            :key="cSubject.id"
            :label="cSubject.title"
            :value="cSubject.id"/>
        </el-select>
      </el-form-item>

      <el-form-item label="课程课时">
        <el-input-number v-model="courseInfo.lessonNum" :min="0" controls-position="right" />
      </el-form-item>

      <el-form-item label="课程简介">
        <tinymce v-model="courseInfo.description"/>
      </el-form-item>

      <el-form-item label="课程封面">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="uploadPath"
          class="avatar-uploader">
          <img v-if="courseInfo.cover" :src="courseInfo.cover" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"/>
        </el-upload>
      </el-form-item>

      <el-form-item label="课程价格">
        <el-input-number v-model="courseInfo.price" :min="0" controls-position="right" />元
      </el-form-item>

    </el-form>
    <div style="text-align:center">
      <el-button :disabled="flag" type="success" @click="saveAndNext">保存并下一步</el-button>
    </div>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import teacherApi from '@/api/teacher'
import subjectApi from '@/api/subject'
import courseApi from '@/api/course'
export default {
  components: {
    Tinymce
  },
  data() {
    return {
      uploadPath: 'http://localhost:8120/admin/oss/upload?module=cover',
      courseInfo: {
        lessonNum: 0,
        subjectParentId: '',
        subjectId: '',
        id: this.$route.params.id,
        description: ''
      },
      teachers: [],
      pSubjects: [],
      flag: false,
      cSubjects: []
    }
  },
  created() {
    this.getTeachers()
    this.getLeveOneSubjects()
    if (this.courseInfo.id) {
      this.fetchCourseInfoById()
    }
  },
  methods: {
    // 回显用method
    fetchCourseInfoById() {
      courseApi.getCourseInfoById(this.courseInfo.id).then(r => {
        this.courseInfo = r.data.courseInfoForm
        this.pSubjects.forEach(p => {
          if (this.courseInfo.subjectParentId === p.id) {
            this.cSubjects = p.children
          }
        })
      })
    },
    saveAndNext() {
      this.disabled = true
      // 判断是更新还是新增
      if (this.courseInfo.id) {
        // 更新
        this.update()
      } else {
        // 新增
        this.save()
      }
      // 向后端发请求存储基本信息。
    },
    update() {
      courseApi.updateById(this.courseInfo).then(r => {
        this.$message.success('更新成功')
        // 跳转到下一步页面
        this.$parent.active = 1
      })
    },
    save() {
      courseApi.saveCourseInfo(this.courseInfo).then(r => {
        this.$parent.courseId = r.data.id
        this.$message.success('新增成功')
        this.$parent.active = 1
      })
    },
    // 获取教师信息
    getTeachers() {
      teacherApi.list().then(r => {
        this.teachers = r.data.list
      })
    },
    getLeveOneSubjects() {
      subjectApi.nestedSubjects().then(r => {
        this.pSubjects = r.data.items
      })
    },
    handleSubjectChange() {
      this.courseInfo.subjectId = ''
      this.pSubjects.forEach(p => {
        if (this.courseInfo.subjectParentId === p.id) {
          this.cSubjects = p.children
        }
      })
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    handleAvatarSuccess(res, file) {
      this.courseInfo.cover = res.data.fileURL
      this.$forceUpdate()
    }
  }
}
</script>
<style>
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 356px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 356px;
    height: 178px;
    display: block;
  }
.tinymce-container {
  position: relative;
  line-height: normal;
}
</style>
